<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="tp.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <header>
        <div class="logo">Mikufans</div>
    </header>
    <main>
        <section class="login">
            <form id="login-form">
                <h2>登录</h2>
                <label for="email">邮箱:</label>
                <input type="email" id="email" required>
                <label for="password">密码:</label>
                <input type="password" id="password" required>
                <button type="submit">登录</button>
            </form>
        </section>
    </main>
    <footer>
        <p>&copy; 2024 Mikufans. 不保留所有权利.</p>
    </footer>
    <script>
        document.getElementById('login-form').addEventListener('submit', (event) => {
            event.preventDefault();
            const email = document.getElementById('email').value;
            const password = document.getElementById('password').value;

            fetch('/login', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ email, password })
            })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                if (data.auth) {
                    localStorage.setItem('token', data.token);
                    window.location.href = '/';
                }
            });
        });
    </script>
</body>
</html>
